package JavaEE初阶.Multi;

import com.sun.tools.javac.Main;

import javax.sound.midi.Soundbank;
import java.util.Random;
import java.util.concurrent.CountDownLatch;

public class Demo08 {
    public static void main(String[] args) throws InterruptedException {
       // 1.此处构造方法中写10，意思是有10个线程/任务
        CountDownLatch latch = new CountDownLatch(10);

        //创建出10个线程负责下载
        for(int i = 0;i < 10; i++){
            int id = i;
            Thread t = new Thread(() ->{
                Random random  =new Random();
                int time = (random.nextInt(5)+1)*100;
                System.out.println("线程" + id + "开始下载");
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("线程" + id + "结束下载");

                //告知countDownLatch  我执行结束了
                latch.countDown();
            });
            t.start();

        }
        //通过这个await操作来等待所有任务结束，也就是countdown被调用10次
        latch.await();
        System.out.println("所有任务都已执行结束了");
    }
}
